package com.qiwo.webshow.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qiwo.common.base.AdminBaseController;
import com.qiwo.common.domain.ContentDO;
import com.qiwo.common.service.ContentService;
import com.qiwo.common.utils.Result;
import com.qiwo.common.utils.cache.Cache;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.Arrays;

/**
 * Copyright
 *
 * @Author:chenwenchao
 * @Date:2018/10/24 0024
 * Description:
 */
@Controller
@RequestMapping("/webshow/content")
public class BrandContentController extends AdminBaseController {

    @Autowired
    private ContentService contentService;

    @GetMapping("/{typeid}")
   /* @RequiresPermissions("common:content:content")*/
    String Content(Model model,@PathVariable("typeid") Long typeId){
        model.addAttribute("typeId",typeId);
        if(typeId==13){
            return "webshow/brand/art/content";
        }
        return "webshow/brand/content";
    }
    @GetMapping("/art/add")
    String artAdd(Model model, @PathParam("pid") Long pid){
        model.addAttribute("typeId",pid);
        return "webshow/brand/art/add";
    }
    @GetMapping("/art/editcontent/{id}")
    String artEdit(@PathVariable("id") Long id, Model model){
        ContentDO contentDO = contentService.selectById(id);
        model.addAttribute("content", contentDO);
        return "webshow/brand/art/edit";
    }
    @GetMapping("/add")
    String add(Model model, @PathParam("pid") Long pid){
        model.addAttribute("typeId",pid);
        return "webshow/brand/add";
    }

    @GetMapping("/editcontent/{id}")
    String editContentId(@PathVariable("id") Long id, Model model){
        ContentDO contentDO = contentService.selectById(id);
        model.addAttribute("content", contentDO);
        return "webshow/brand/edit";
    }

    @GetMapping("/siteinfo/{id}")
    String siteinfo(@PathVariable("id") Long id, Model model){
        ContentDO content = contentService.findOneByKv("type_id",id);
        if(content==null){
            content=new ContentDO();
            content.setTypeId(id);
        }
        model.addAttribute("content", content);
        return "webshow/brand/siteinfo";
    }

    @GetMapping("/list")
    @ResponseBody
    public Result<Page<ContentDO>> list(ContentDO contentDO){
        Wrapper<ContentDO> contentDOWrapper=new EntityWrapper<>();
        contentDOWrapper.orderBy("sort",true);
        if(contentDO.getTypeId()!=null&&contentDO.getTypeId()!=0){
            contentDOWrapper.eq("type_id",contentDO.getTypeId());
        }
        Page<ContentDO> page=contentService.selectPage(getPage(ContentDO.class),contentDOWrapper);
        return  Result.ok(page);
    }

    @GetMapping("/edit/{typeid}")
    @RequiresPermissions("webshow:content:edit")
    String edit(@PathVariable("typeid") Long typeId, Model model){
        ContentDO content = contentService.findOneByKv("type_id",typeId);
        if(content==null){
            content=new ContentDO();
            content.setTypeId(typeId);
        }
        model.addAttribute("content", content);
        return "webshow/brand/about";
    }
    @GetMapping("/editbrand/{typeid}")
    @RequiresPermissions("webshow:content:edit")
    String editbrand(@PathVariable("typeid") Long typeId, Model model){
        ContentDO content = contentService.findOneByKv("type_id",typeId);
        if(content==null){
            content=new ContentDO();
            content.setTypeId(typeId);
        }
        model.addAttribute("content", content);
        return "webshow/brand/brand";
    }

    @GetMapping("/editzhuxian/{typeid}")
    @RequiresPermissions("webshow:content:editzhuxian")
    String editzhuxian(@PathVariable("typeid") Long typeId, Model model){
        ContentDO content = contentService.findOneByKv("type_id",typeId);
        if(content==null){
            content=new ContentDO();
            content.setTypeId(typeId);
        }
        model.addAttribute("content", content);
        return "webshow/brand/series";
    }
    /**
     * 删除
     */
    @PostMapping( "/remove")
    @ResponseBody
    public Result<String> remove(Long id){
        contentService.deleteById(id);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping( "/batchRemove")
    @ResponseBody
    public Result<String> remove(@RequestParam("ids[]") Integer[] ids){
        contentService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }
    @GetMapping("/editour/{typeid}")
    @RequiresPermissions("webshow:content:editour")
    String editour(@PathVariable("typeid") Long typeId, Model model){
        ContentDO content = contentService.findOneByKv("type_id",typeId);
        if(content==null){
            content=new ContentDO();
            content.setTypeId(typeId);
        }
        model.addAttribute("content", content);
        return "webshow/brand/aboutOur";
    }

    @GetMapping("/editmap/{typeid}")
/*    @RequiresPermissions("webshow:content:editmap")*/
    String editmap(@PathVariable("typeid") Long typeId, Model model){
        ContentDO content = contentService.findOneByKv("type_id",typeId);
        if(content==null){
            content=new ContentDO();
            content.setTypeId(typeId);
        }
        model.addAttribute("content", content);
        return "webshow/brand/aboutMap";
    }


    @GetMapping("/editImg/{typeid}")
    @RequiresPermissions("webshow:content:editImg")
    String editImg(@PathVariable("typeid") Long typeId, Model model){
        ContentDO content = contentService.findOneByKv("type_id",typeId);
        if(content==null){
            content=new ContentDO();
            content.setTypeId(typeId);
        }
        model.addAttribute("content", content);
        return "webshow/brand/aboutImage";
    }

    @GetMapping("/edithome/{typeid}")
    @RequiresPermissions("webshow:content:edithome")
    String edithome(@PathVariable("typeid") Long typeId, Model model){
        ContentDO content = contentService.findOneByKv("type_id",typeId);
        if(content==null){
            content=new ContentDO();
            content.setTypeId(typeId);
        }
        model.addAttribute("content", content);
        return "webshow/brand/home";
    }
    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    public Result<String> update(ContentDO content){
        if(content.getId()!=null) {
            if(content.getTypeId()==18){
                Cache.remove("viedoUrl");
            }
            boolean update = contentService.updateById(content);
            return update ? Result.ok() : Result.fail();
        }else{
            ContentDO content2 = contentService.findOneByKv("type_id",content.getTypeId());
            if(content2!=null){
                content.setId(content2.getId());
                boolean update = contentService.updateById(content);
                return update ? Result.ok() : Result.fail();
            }
            contentService.insert(content);
            return Result.ok();
        }
    }
}
